package org.fly.utils;


import org.fly.enums.TimeUnit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by rongzhao.zhu on 2017/11/2.
 */
public class TimeUtil {

    public static String linkStr ="-";
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static String convert(long time, TimeUnit timeUnit){
        String  showName ="";
        Date date = new Date(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int realMonth =  month+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int quarterly = month/3+1;
        switch (timeUnit){
            case day:showName =year+"年"+String.format("%02d",realMonth)+"月"+String.format("%02d",day);
                break;
            case month:showName=year+"年"+String.format("%02d",realMonth)+"月";break;
            case quarterly:showName=year+"年"+quarterly+"季度"; break;
            case year:showName=String.valueOf(year);

        }
        return showName;
    }

    /**
     *
     * 构建list
     * @param startTime
     * @param endTime
     * @param timeUnit
     * @return
     */
    public static List<String> buildFullList(String startTime,String endTime,TimeUnit timeUnit){
        List<String> initList = null;
        String end =   dayCovert(endTime,timeUnit);
        Set<String> initSet = new TreeSet<>();
        try{
            Date startDate = sdf.parse(startTime);
            Date next = startDate;
            Date endDate = sdf.parse(endTime);
            Calendar calendar = Calendar.getInstance();
            while (next.compareTo(endDate)<=0){
                initSet.add(convert(next.getTime(),timeUnit));
                calendar.setTime(next);
                switch (timeUnit){
                    case year:
                        calendar.add(Calendar.YEAR,1);
                        next = calendar.getTime();
                        break;
                    case quarterly:
                        calendar.add(Calendar.MONTH,3);
                        next = calendar.getTime();
                        break;
                    case month:
                        calendar.add(Calendar.MONTH,1);
                        next = calendar.getTime();
                        break;
                    case day:
                        calendar.add(Calendar.DATE,1);
                        next = calendar.getTime();
                        break;

               }
            }
            initSet.add(end);
            initList = initSet.stream().collect(Collectors.toList());

        }catch (Exception e){
            e.printStackTrace();
        }
        return initList;
    }

    public static List<String> buildFullList(long startTime,long endTime,TimeUnit timeUnit){
        List<String> initList = null;
        String start=  convert(startTime,timeUnit);
        String end =   convert(endTime,timeUnit);
        Set<String> initSet = new TreeSet<>();
        try{
            Date startDate = new Date(startTime);
            Date next = startDate;
            Date endDate = new Date(endTime);
            Calendar calendar = Calendar.getInstance();
            while (next.compareTo(endDate)<=0){
                initSet.add(convert(next.getTime(),timeUnit));
                calendar.setTime(next);
                switch (timeUnit){
                    case year:
                        calendar.add(Calendar.YEAR,1);
                        next = calendar.getTime();
                        break;
                    case quarterly:
                        calendar.add(Calendar.MONTH,3);
                        next = calendar.getTime();
                        break;
                    case month:
                        calendar.add(Calendar.MONTH,1);
                        next = calendar.getTime();
                        break;
                    case day:
                        calendar.add(Calendar.DATE,1);
                        next = calendar.getTime();
                        break;

                }
            }
            initSet.add(end);
            initList = initSet.stream().collect(Collectors.toList());

        }catch (Exception e){
            e.printStackTrace();
        }

        return initList;
    }

    /**
     *
     * @param dayStr yyyy-MM-dd 格式
     * @param timeUnit
     * @return
     */
    public static String dayCovert(String dayStr,TimeUnit timeUnit){
        String result = null;
        try {
           Date date =  sdf.parse(dayStr);
            result = convert(date.getTime(),timeUnit);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }




    public static void main(String[] args){
        TimeUtil.buildFullList("2010-01-01","2011-10-10",TimeUnit.month);
    }
}
